package com.touchalachs.game;

import android.graphics.Canvas;
import android.graphics.Point;
import android.view.MotionEvent;

import com.touchalachs.core.Drawable;
import com.touchalachs.core.Touchable;
import com.touchalachs.core.ui.Button;
import com.touchalachs.core.ui.ButtonListener;
import com.touchalachs.core.ui.ImageButton;
import com.touchalachs.core.ui.TippableBasicUIElement;

public class BottomPanel implements Drawable, Touchable, ButtonListener {
	private final WorkerInfoPopup workerPopup;
	private final StatsPopup statsPopup;
	private final Calculator calc;
	private final ImageButton[] workerButtons;
	private final Button statsButton;

	public BottomPanel(Point screenSize, Calculator calc, WorkerInfoPopup workerPopup, StatsPopup statsPopup) {
		this.workerPopup = workerPopup;
		this.statsPopup = statsPopup;
		this.calc = calc;
		workerButtons = new ImageButton[] { 
				new ImageButton( 0, 88, 20, 12, screenSize, Res.WORKER_ICONS[0], this),
				new ImageButton(20, 88, 20, 12, screenSize, Res.WORKER_ICONS[1], this),
				new ImageButton(40, 88, 20, 12, screenSize, Res.WORKER_ICONS[2], this),
				new ImageButton(60, 88, 20, 12, screenSize, Res.WORKER_ICONS[3], this) };
		statsButton = new Button(80, 88, 20, 12, Res.STATS, screenSize, this);
	}

	@Override
	public void draw(Canvas canvas) {
		for (int i = 0; i < workerButtons.length; i++) {
			workerButtons[i].draw(canvas);
		}
		statsButton.draw(canvas);
	}

	@Override
	public boolean onTouchEvent(MotionEvent event) {
		if (event.getAction() == MotionEvent.ACTION_DOWN) {
			for (int b = 0; b < workerButtons.length; b++) {
				if (workerButtons[b].onTouchEvent(event)) {
					int owned = calc.getWorkerCount(b);
					int cost = Balancing.getCost(b, owned);
					workerPopup.showPopup(Res.WORKER_TITLES[b], Res.WORKER_DESCRIPTIONS[b], b, owned, cost);
					return true;
				}
			}
			if (statsButton.onTouchEvent(event)){
				statsPopup.show();
				return true;
			}
		}
		return false;
	}

	@Override
	public boolean onTipp(TippableBasicUIElement source) {
		return true;
	}
}
